package snippets;

/*
 * Browser example snippet: modify DOM (executing javascript)
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.1
 */
import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet161 {
	public static void main(String [] args) {
//		final String html = "<html><title>Snippet</title><body><p id='myid'>Best Friends</p><p id='myid2'>Cat and Dog</p></body></html>";
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		final Browser browser;
		try {
			browser = new Browser(shell, SWT.BORDER);
		} catch (SWTError e) {
			System.out.println("Could not instantiate Browser: " + e.getMessage());
			return;
		}
		Composite comp = new Composite(shell, SWT.NONE);
		comp.setLayout(new FillLayout(SWT.VERTICAL));
		final Text text = new Text(comp, SWT.MULTI);
//		String script = "var newNode = document.createElement('P'); \r\n"+
//		"var text = document.createTextNode('At least when I am around');\r\n"+
//		"newNode.appendChild(text);\r\n"+
//		"document.getElementById('myid').appendChild(newNode);\r\n"+
//		"\r\n"+
//		"document.bgColor='yellow';";
		String script = "document.bgColor='yellow';";
		text.setText(script);
		final Button button = new Button(comp, SWT.PUSH);
		button.setText("Execute Script");
		button.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				boolean result = browser.execute(text.getText());
				if (!result) {
					/* Script may fail or may not be supported on certain platforms. */
					System.out.println("Script was not executed.");
				}
			}
		});
		browser.setText(html);
//		browser.setUrl("http://10.8.1.17");
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	
	static String html = 
		"<html>\n"+
		"<head>\n"+
		"<script type=\"text/javascript\" language=\"text/javascript\">\n"+
		"<!--\n"+
		"\n"+
		"function avoidFraming()\n"+
		"{\n"+
		"	if (top.location.href != window.location.href)\n"+
		"	{\n"+
		"		top.location.href = \"login.htm\";\n"+
		"	}\n"+
		"}\n"+
		"\n"+
		"// -->\n"+
		"</script>\n"+
		"<title>Login</title>\n"+
		"</head>\n"+
		"<body onLoad=\"avoidFraming();document.getElementById('username').focus();\">\n"+
		"<form action=\"login.dxml\" method=\"post\"> \n"+
		"<table align=\"center\" style=\"font-family:arial; font-size:12; border:1px solid #000000;\" width=\"95\"> \n"+
		"  <tr>\n"+
		"    <td colspan=\"3\" align=\"center\" bgcolor=\"#ff0000\" width=\"320\"><b><font color=\"#FFFFFF\">LOGIN</font></b></td>\n"+
		"  </tr> \n"+
		"  <tr>\n"+
		"\n"+
		"    <td align=\"right\" width=\"62\">Username: </td><td colspan=\"2\" width=\"254\">\n"+
		"      <input type=\"text\" name=\"username\" id=\"username\" size=\"38\" maxlength=\"32\"/>\n"+
		"	</td>\n"+
		"  </tr> \n"+
		"  <tr>\n"+
		"    <td align=\"right\" width=\"62\">Password: </td>\n"+
		"	<td colspan=\"2\" width=\"254\">\n"+
		"      <input type=\"password\" name=\"password\" size=\"38\" maxlength=\"32\"/>\n"+
		"\n"+
		"	</td>\n"+
		"  </tr> \n"+
		"  <tr>\n"+
		"    <td align=\"center\" width=\"62\"> </td>\n"+
		"    <td align=\"right\" width=\"136\"> </td>\n"+
		"    <td align=\"right\" width=\"146\">\n"+
		"	  <input type=\"submit\" value=\"      Login      \"/>\n"+
		"	</td>\n"+
		"\n"+
		"  </tr> \n"+
		"</table> \n"+
		"</form> \n"+
		"</body>\n"+
		"</html>\n";


}

